﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;
using System.Drawing;

namespace School.Pages.BOOKS
{
    public partial class SRV_BOOKS_RECEIVINGByCode : System.Web.UI.Page
    {
        private Hashtable table;
        protected void Page_Load(object sender, EventArgs e)
        {
            if ((IQueryable<int>)Session["privilage"] != null)
            {
                var x = (IQueryable<int>)Session["privilage"];

                if (x.Contains(1107) == true)
                {
                    if (!this.Page.IsPostBack)
                        table = new Hashtable();
                    else
                        table = (Hashtable)ViewState["table"];
                    lblError.Text = "";
                }
                else
                    Response.Redirect("../organization/no_privelege.aspx");
            }
            else
                Response.Redirect("../organization/no_privelege.aspx");
        }

        protected void btnAddQty_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(txtCode.Text) && !string.IsNullOrEmpty(txtQty.Text))
            {
                if (table == null)
                    table = new Hashtable();
                foreach (DataKey item in gvData.DataKeys)
                {
                    if (item.Value.ToString() == txtCode.Text)
                    {
                        lblError.ForeColor = Color.Red;
                        lblError.Text = "هذا الكود مدخل من قبل ";
                        return;
                    }
                }
                table.Add(txtCode.Text, txtQty.Text);
                ViewState["table"] = table;
                gvData.DataSource = table;
                gvData.DataBind();
                txtCode.Text = "";
                txtQty.Text = "";

            }
        }

        protected void btnSaveChanges_Click(object sender, EventArgs e)
        {
            table = (Hashtable)ViewState["table"];
            if (table == null)
            {
                lblError.ForeColor = Color.Red;
                lblError.Text = "ادخل قيم اولا";
              
            }
            else
                if (School.Service.BOOKS.SRV_BOOKS_TRANSACTIONS.SRV_TRANSACTIONInsertByCode(table))
                {
                    lblError.ForeColor = Color.Green;
                    lblError.Text = "تمت الاضافه بنجاح";
                    txtCode.Text = "";
                    txtQty.Text = "";
                    ClearData();
                }
                else
                {
                    lblError.ForeColor = Color.Red;
                    lblError.Text = "هناك خطا فى الكود من فضلك تاكد من الكود المدخل";
                }
        }

        private void ClearData()
        {
            ((Hashtable)ViewState["table"]).Clear();
            gvData.DataSource = (Hashtable)ViewState["table"];
            gvData.DataBind();
        }

        protected void gvData_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "DoDelete")
            {
                GridViewRow selectedRow = (GridViewRow)((LinkButton)e.CommandSource).NamingContainer;
                DataKey Key = gvData.DataKeys[selectedRow.RowIndex];
                gvData.DeleteRow(selectedRow.RowIndex);
                table = (Hashtable)ViewState["table"];
                table.Remove(Key.Value);
                ViewState["table"] = table;
                gvData.DataSource = table;
                gvData.DataBind();
            }
        }

        protected void gvData_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {

        }
    }
}